Skip to content

fix(import): escape triple-quotes in collaborationInstruction to prevent docstring injection#1329

Merged
padmak30 merged 2 commits into
mainfrom
fix/import
May 20, 2026
Merged

fix(import): escape triple-quotes in collaborationInstruction to prevent docstring injection#1329
padmak30 merged 2 commits into
mainfrom
fix/import

Conversation

@padmak30
Copy link
Copy Markdown
Contributor

@padmak30 padmak30 commented May 20, 2026

Description

collaborationInstruction is free-form text that gets embedded inside a
Python triple-quoted docstring ("""...""") in the generated main.py.
Using only escapePySingleQuote left """ unescaped, allowing a malicious
collaborator instruction to break out of the docstring and inject
executable Python code into the generated file

Fix: apply escapePyTripleQuote (which escapes """ → """) when building the collaboratorDescriptions string.

@padmak30 padmak30 requested a review from a team May 20, 2026 21:18
@github-actions github-actions Bot added size/s PR size: S agentcore-harness-reviewing AgentCore Harness review in progress labels May 20, 2026
@agentcore-devx-automation agentcore-devx-automation Bot added the claude-security-reviewing Claude Code /security-review in progress label May 20, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Package Tarball

aws-agentcore-0.14.1.tgz

How to install

gh release download pr-1329-tarball --repo aws/agentcore-cli --pattern "*.tgz" --dir /tmp/pr-tarball
npm install -g /tmp/pr-tarball/aws-agentcore-0.14.1.tgz

Copy link
Copy Markdown

@agentcore-cli-automation agentcore-cli-automation left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching the docstring injection — the security fix is correct in spirit, but the way the two escape helpers are chained introduces a new bug for any input containing backslashes.

escapePySingleQuote already does \\\\\\, and escapePyTripleQuote does \\\\\\ again. Composing them therefore quadruples every backslash.

I verified by simulating in Python with the same code paths:

User input:                C:\path\to\file
After this PR (chained):   C:\\\\path\\\\to\\\\file   (in source) → C:\\path\\to\\file (at runtime)
Before this PR:            C:\\path\\to\\file         (in source) → C:\path\to\file   (at runtime, correct)

So a collaborationInstruction containing backslashes (Windows paths, regex patterns, JSON-escaped strings, etc.) will be silently corrupted in the generated docstring after this change.

A couple of options to fix:

  • Apply only escapePyTripleQuote. The value lives in a triple-quoted docstring; the surrounding '…' is just decorative text, not Python syntax. Escaping """ and backslashes is sufficient — ' doesn't need escaping inside a triple-quoted string.
  • Compose them without re-escaping backslashes. E.g. introduce a single helper that escapes \, then ', then """, then \n in one pass, or strip the redundant \\\\\\ step from one of the helpers when chained.

Either way, please add a test with a backslash-bearing payload (e.g. 'C:\\path\\to\\file' or 'pattern: \\d+') that asserts the runtime docstring (or at least the emitted source) preserves the original backslash count, so this regression is caught.

A nit on the existing test: the negative assertion not.toMatch(/""".*"""\s*"""/s) is quite weak. Asserting that mainPyContent still parses as valid Python (or at least that the dangerous tokens like import subprocess don't appear at top-level / outside the docstring) would give a stronger guarantee that injection is actually neutralized.

Comment thread src/cli/operations/agent/import/base-translator.ts Outdated
@github-actions github-actions Bot removed the agentcore-harness-reviewing AgentCore Harness review in progress label May 20, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 20, 2026

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 43.87% 9346 / 21299
🔵 Statements 43.12% 9917 / 22996
🔵 Functions 40.38% 1610 / 3987
🔵 Branches 40.61% 6082 / 14976
Generated in workflow #3171 for commit d7ff8a1 by the Vitest Coverage Report Action

@agentcore-devx-automation
Copy link
Copy Markdown
Contributor

Claude Security Review: no high-confidence findings. (run)

@agentcore-devx-automation agentcore-devx-automation Bot removed the claude-security-reviewing Claude Code /security-review in progress label May 20, 2026
…ent docstring injection

collaborationInstruction is free-form text that gets embedded inside a
Python triple-quoted docstring ("""...""") in the generated main.py.
Using only escapePySingleQuote left """ unescaped, allowing a malicious
collaborator instruction to break out of the docstring and inject
executable Python code into the generated file (HackerOne #3733333).

Fix: use escapePyTripleQuote (escapes """ and \) instead of the previous
escapePySingleQuote for collaborationInstruction. Chaining both helpers
was also incorrect as it doubled backslash escaping.
agentName is not affected — Bedrock enforces [0-9a-zA-Z_-] on that field.
@github-actions github-actions Bot removed the size/s PR size: S label May 20, 2026
@github-actions github-actions Bot added the size/s PR size: S label May 20, 2026
@agentcore-devx-automation agentcore-devx-automation Bot added the claude-security-reviewing Claude Code /security-review in progress label May 20, 2026
@agentcore-devx-automation
Copy link
Copy Markdown
Contributor

Claude Security Review: no high-confidence findings. (run)

@agentcore-devx-automation agentcore-devx-automation Bot removed the claude-security-reviewing Claude Code /security-review in progress label May 20, 2026
@padmak30 padmak30 merged commit ae1b932 into main May 20, 2026
28 checks passed
@padmak30 padmak30 deleted the fix/import branch May 20, 2026 22:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/s PR size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants